<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>_routing field | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'mapping-routing-field.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/mapping-routing-field.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/mapping-routing-field.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/mapping-routing-field.html" rel="nofollow" target="_blank">../en/mapping-routing-field.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">Mapping</a></span>
»
<span class="breadcrumb-link"><a href="mapping-fields.html">Meta-Fields</a></span>
»
<span class="breadcrumb-node"><code class="literal">_routing</code> field</span>
</div>
<div class="navheader">
<span class="prev">
<a href="mapping-meta-field.html">« <code class="literal">_meta</code> field</a>
</span>
<span class="next">
<a href="mapping-source-field.html"><code class="literal">_source</code> field »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="mapping-routing-field"></a><code class="literal">_routing</code> field<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/fields/routing-field.asciidoc">edit</a>
</h2>
</div></div></div>
<p>A document is routed to a particular shard in an index using the following
formula:</p>
<pre class="literallayout">shard_num = hash(_routing) % num_primary_shards</pre>

<p>The default value used for <code class="literal">_routing</code> is the document’s <a class="xref" href="mapping-id-field.html" title="_id field"><code class="literal">_id</code></a>.</p>
<p>Custom routing patterns can be implemented by specifying a custom <code class="literal">routing</code>
value per document.  For instance:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index/_doc/1?routing=user1&amp;refresh=true <a id="CO326-1"></a><i class="conum" data-value="1"></i>
{
  "title": "This is a document"
}

GET my_index/_doc/1?routing=user1 <a id="CO326-2"></a><i class="conum" data-value="2"></i></pre>
</div>
<div class="console_widget" data-snippet="snippets/735.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO326-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>This document uses <code class="literal">user1</code> as its routing value, instead of its ID.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO326-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>The same <code class="literal">routing</code> value needs to be provided when
<a class="xref" href="docs-get.html" title="Get API">getting</a>, <a class="xref" href="docs-delete.html" title="Delete API">deleting</a>, or <a class="xref" href="docs-update.html" title="Update API">updating</a>
the document.</p>
</td>
</tr>
</table>
</div>
<p>The value of the <code class="literal">_routing</code> field is accessible in queries:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET my_index/_search
{
  "query": {
    "terms": {
      "_routing": [ "user1" ] <a id="CO327-1"></a><i class="conum" data-value="1"></i>
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/736.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO327-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Querying on the <code class="literal">_routing</code> field (also see the <a class="xref" href="query-dsl-ids-query.html" title="IDs"><code class="literal">ids</code> query</a>)</p>
</td>
</tr>
</table>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_searching_with_custom_routing"></a>Searching with custom routing<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/fields/routing-field.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Custom routing can reduce the impact of searches.  Instead of having to fan
out a search request to all the shards in an index, the request can be sent to
just the shard that matches the specific routing value (or values):</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET my_index/_search?routing=user1,user2 <a id="CO328-1"></a><i class="conum" data-value="1"></i>
{
  "query": {
    "match": {
      "title": "document"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/737.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO328-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>This search request will only be executed on the shards associated with the <code class="literal">user1</code> and <code class="literal">user2</code> routing values.</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_making_a_routing_value_required"></a>Making a routing value required<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/fields/routing-field.asciidoc">edit</a>
</h3>
</div></div></div>
<p>When using custom routing, it is important to provide the routing value
whenever <a class="xref" href="docs-index_.html" title="Index API">indexing</a>, <a class="xref" href="docs-get.html" title="Get API">getting</a>,
<a class="xref" href="docs-delete.html" title="Delete API">deleting</a>, or <a class="xref" href="docs-update.html" title="Update API">updating</a> a document.</p>
<p>Forgetting the routing value can lead to a document being indexed on more than
one shard.  As a safeguard, the <code class="literal">_routing</code> field can be configured to make a
custom <code class="literal">routing</code> value required for all CRUD operations:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index2
{
  "mappings": {
    "_routing": {
      "required": true <a id="CO329-1"></a><i class="conum" data-value="1"></i>
    }
  }
}

PUT my_index2/_doc/1 <a id="CO329-2"></a><i class="conum" data-value="2"></i>
{
  "text": "No routing value provided"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/738.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO329-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Routing is required for all documents.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO329-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>This index request throws a <code class="literal">routing_missing_exception</code>.</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_unique_ids_with_custom_routing"></a>Unique IDs with custom routing<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/fields/routing-field.asciidoc">edit</a>
</h3>
</div></div></div>
<p>When indexing documents specifying a custom <code class="literal">_routing</code>, the uniqueness of the
<code class="literal">_id</code> is not guaranteed across all of the shards in the index. In fact,
documents with the same <code class="literal">_id</code> might end up on different shards if indexed with
different <code class="literal">_routing</code> values.</p>
<p>It is up to the user to ensure that IDs are unique across the index.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="routing-index-partition"></a>Routing to an index partition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/fields/routing-field.asciidoc">edit</a>
</h3>
</div></div></div>
<p>An index can be configured such that custom routing values will go to a subset of the shards rather
than a single shard. This helps mitigate the risk of ending up with an imbalanced cluster while still
reducing the impact of searches.</p>
<p>This is done by providing the index level setting <a class="xref" href="index-modules.html#routing-partition-size"><code class="literal">index.routing_partition_size</code></a> at index creation.
As the partition size increases, the more evenly distributed the data will become at the
expense of having to search more shards per request.</p>
<p>When this setting is present, the formula for calculating the shard becomes:</p>
<pre class="literallayout">shard_num = (hash(_routing) + hash(_id) % routing_partition_size) % num_primary_shards</pre>

<p>That is, the <code class="literal">_routing</code> field is used to calculate a set of shards within the index and then the
<code class="literal">_id</code> is used to pick a shard within that set.</p>
<p>To enable this feature, the <code class="literal">index.routing_partition_size</code> should have a value greater than 1 and
less than <code class="literal">index.number_of_shards</code>.</p>
<p>Once enabled, the partitioned index will have the following limitations:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
Mappings with <a class="xref" href="parent-join.html" title="Join datatype"><code class="literal">join</code> field</a> relationships cannot be created within it.
</li>
<li class="listitem">
All mappings within the index must have the <code class="literal">_routing</code> field marked as required.
</li>
</ul>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="mapping-meta-field.html">« <code class="literal">_meta</code> field</a>
</span>
<span class="next">
<a href="mapping-source-field.html"><code class="literal">_source</code> field »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>